package com.wuba.rxjavademo.tzx.merge;

import com.wuba.rxjavademo.tzx.common.Base;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.TimeUnit;

import rx.Observable;
import rx.functions.Func1;

/**
 * Created by tanzhenxing
 * Date: 2016/6/28.
 * Description:
 */
public class RxMerge extends Base {
    public static void main(String[] args) {
        RxMerge merge = new RxMerge();
        merge.run();
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void run() {
        //Observable.merge(getObservableTimer(),getObservableTimer2())
        //        .subscribe(Base::print);
        Observable.merge(Observable.just(1), Observable.just(2,3,4))
                .subscribe(Base::print);
    }

    public Observable<Long> getObservableTimer() {
        return Observable.timer(1,1, TimeUnit.SECONDS)
                .map(num -> num*100);
    }

    public Observable<Long> getObservableTimer2() {
        return Observable.timer(2000, 500, TimeUnit.MILLISECONDS)
                .map(num -> num*10);
    }

    public Observable<Integer> getObservable() {
        return Observable.<Integer>create(subscriber -> {
                        for (int i = 1; i <= 5; i++) {
                            subscriber.onNext(i);
                            try {
                                Thread.sleep(500);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }
                        subscriber.onCompleted();
                    });
    }

    public Observable<Integer> getReverseObserable() {
        return Observable.<Integer>create(subscriber -> {
                        for (int i = 11; i <= 19; i++) {
                            subscriber.onNext(i);
                            try {
                                Thread.sleep(400);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }
                        subscriber.onCompleted();
                    });
    }
}
